#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import configparser
import winshell
from win32com.client import Dispatch

def create_shortcut():
    try:
        # 读取配置文件获取端口号
        config = configparser.ConfigParser()
        config_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'config.ini')
        config.read(config_path, encoding='utf-8')
        
        # 获取端口号，默认为8000
        port = config.get('Server', 'port', fallback='8000')
        
        # 创建快捷方式
        desktop = winshell.desktop()
        shortcut_path = os.path.join(desktop, "云打印系统.lnk")
        
        # 创建快捷方式对象
        shell = Dispatch('WScript.Shell')
        shortcut = shell.CreateShortCut(shortcut_path)
        
        # 设置快捷方式属性
        shortcut.TargetPath = f"http://localhost:{port}"
        # 使用默认浏览器打开链接
        shortcut.IconLocation = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static', 'favicon.ico')
        shortcut.Description = "打开云打印系统Web界面"
        shortcut.WorkingDirectory = os.path.dirname(os.path.abspath(__file__))
        shortcut.Save()
        
        print(f"桌面快捷方式已创建: {shortcut_path}")
        print(f"云打印系统将在浏览器中打开，访问地址: http://localhost:{port}")
        return True
    
    except Exception as e:
        print(f"创建快捷方式时出错: {str(e)}")
        return False

if __name__ == "__main__":
    sys.exit(0 if create_shortcut() else 1) 